#! /usr/bin/python
# encoding=utf-8
# Created by Fenglu Niu on 2025/2/28 22:23
from typing import Any

import common

name = 'searcher'


class Searcher(object):

    def __init__(self, keyword: str = None, cookie: str = None, user_agent: str = None, num: int = 10):
        self.__keyword = keyword
        self.__cookie = cookie
        self.__user_agent = user_agent
        self.__num = num

    @property
    def keyword(self):
        return self.__keyword

    @keyword.setter
    def keyword(self, keyword: str):
        self.__keyword = keyword

    @property
    def cookie(self):
        return self.__cookie

    @cookie.setter
    def cookie(self, cookie: str):
        self.__cookie = cookie

    @property
    def user_agent(self):
        return self.__user_agent

    @user_agent.setter
    def user_agent(self, user_agent: str):
        self.__user_agent = user_agent

    @property
    def num(self):
        return self.__num

    @num.setter
    def num(self, num: int):
        self.__num = num

    def search(self):
        pass

class Context(object):
    @staticmethod
    def do_search(engine: str, keyword: str, cookie: str, user_agent: str, num: int) -> Any | None:
        try:
            e = common.Engine(engine)
        except ValueError:
            common.get_logger().error(f'搜索引擎[{engine}]不存在')
            return None
        from searcher.baidu import BaiduSearcher

        deep_searcher = eval(f'{e.name}Searcher()')
        deep_searcher.keyword = keyword
        deep_searcher.cookie = cookie
        deep_searcher.user_agent = user_agent
        deep_searcher.num = num
        return deep_searcher.search()
